DM 'CLEAR LOG; CLEAR OUTPUT;';
libname duration 'path to be completed path to duration data sets';
title '                   ';


/******************************************************************/
/* Compute Frequency of Wage agreements (without NMW)***************************/
/******************************************************************/
/*period 1994-2001*/

/*duree_effet_br9401 : contains observed durations between two industry-level effects*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
ann_eff: year of agreemente effect
code_dept: local departement
mois_eff: month of agreement effect
t_ents_yp: size class
*/

/*duree_eff_ents9401 : contains observed durations between two firm-level effects*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
ann_eff: year of agreement
code_dept: local departement
mois_eff: month of agreement
t_ents_yp: size class*/

/*duree_eff_debfin9401 : contains dates of beginning and end of sample firms*/
/*main variables:
D23: sector
SIREN: firm identifier
YP: number of employees
code_dept: local departement
t_ents_yp: size class
indic_deb date of first observation of the firm in the sample
indic_fin date of last observation of the firm in the sample
*/ 


/****************keep all dates of effects of wage agreements at the industry-level for all firms***********/
data duree_effet_br9401_f;
set rev.duree_effet_br9401;
indic_eff_br=1;
drop an l_an l_mois;
run;
/****************keep all dates of effects of wage agreements at the firm-level for all firms***********/
data duree_eff_ents9401_f;
set rev.duree_eff_ents9401;
indic_eff_e=1;
drop an l_an l_mois;
run;
/****************keep the first and the last observation for all firms************/
data duree_eff_debfin9401_f;
set rev.duree_eff_debfin9401;
indic_eff=0;
drop an l_an l_mois;
run;


/*stack three data sets*********************/
data duree_effet_br_ents9401_f;
set duree_effet_br9401_f duree_eff_ents9401_f duree_eff_debfin9401_f; 
drop an l_an l_mois;
run;
data duree_effet_br_ents9401_f;
set duree_effet_br_ents9401_f;
if siren='         ' then delete;
run;


proc sort data=duree_effet_br_ents9401_f;
by siren ann_eff mois_eff ;
run;
/*define censoring (left or right) and define a variable "indic_effc" equal to one if there is an effect of a wage agreement (industry or firm)*/
data duree_effet_br_ents9401_f;
set duree_effet_br_ents9401_f;
indic_cd=0;
indic_cg=0;
if indic_deb=1 then indic_cg=1;
if indic_fin=1 then indic_cd=1;

if indic_eff_br=. then  indic_eff_br=0;
if indic_eff_e=. then  indic_eff_e=0;
if indic_eff_e=0 and indic_eff_br=0 then indic_eff=0;
if indic_eff_e=1 or indic_eff_br=1 then indic_eff=1;

run;
/*keep only useful variables : sector (D23), firm identifier (siren), year of agreement (an_eff), geographical location (code_dept), left and right censoring dummy, 
month of the agreement (mois_eff), size of the firm in classes (t_ents_yp), nb of employees (yp), dummy for effect of an agreement at the industry, firm and both levels*/
data duree_effet_br_ents9401_f;
set duree_effet_br_ents9401_f;
keep D23 SIREN ann_eff code_dept indic_cd indic_cg  mois_eff t_ents_yp yp indic_eff_br indic_eff_e indic_eff; 
run;


proc sort data=duree_effet_br_ents9401_f nodupkeys;
by siren ann_eff mois_eff;
run;
data duree_effet_br_ents9401_f;
set duree_effet_br_ents9401_f;
rename ann_eff=an;
rename mois_eff=mois; 
run;


/*duration.duree_effets9401: contain all firms coming from the FIBEN data set (with and without effects)- year frequency*/
data freq_effets9401;
set rev.duree_effets9401;
keep D23 SIREN an code_dept t_ents_yp yp; 
run;
proc sort data=freq_effets9401;
by siren an;
run;
/*construct a data set containing all observations BUT at a monthly frequency*******/

data freq_essai1;set freq_effets9401;mois=1;run;
data freq_essai2;set freq_effets9401;mois=2;run;
data freq_essai3;set freq_effets9401;mois=3;run;
data freq_essai4;set freq_effets9401;mois=4;run;
data freq_essai5;set freq_effets9401;mois=5;run;
data freq_essai6;set freq_effets9401;mois=6;run;
data freq_essai7;set freq_effets9401;mois=7;run;
data freq_essai8;set freq_effets9401;mois=8;run;
data freq_essai9;set freq_effets9401;mois=9;run;
data freq_essai10;set freq_effets9401;mois=10;run;
data freq_essai11;set freq_effets9401;mois=11;run;
data freq_essai12;set freq_effets9401;mois=12;run;
data freq_effets9401_tot;
set freq_essai1 freq_essai2 freq_essai3 freq_essai4 freq_essai5 freq_essai6 freq_essai7 freq_essai8 freq_essai9 freq_essai10 freq_essai11 freq_essai12;
run;
proc sort data=freq_effets9401_tot;
by siren an mois;
run;

/*merge data set containing all effects of agreements with year and month with monthly frequency data set containing all firms observations*/
data rev.freq_effets9401_totb;
merge freq_effets9401_tot(in=in1) duree_effet_br_ents9401_f;
by siren an mois;
if in1;
run;

data rev.freq_effets9401_totb;
set rev.freq_effets9401_totb;
l_siren=lag(siren);
l_t=lag(t_ents_yp);
l_d=lag(code_dept);
l_yp=lag(yp);
l_d23=lag(d23);
if l_siren=siren and t_ents_yp=. then t_ents_yp=l_t;
if l_siren=siren and code_dept='  ' then code_dept=l_d;
if l_siren=siren and yp=. then yp=l_yp;
if l_siren=siren and d23='    ' then d23=l_d23;
run;
data rev.freq_effets9401_totb;
set  rev.freq_effets9401_totb;
drop l_siren l_an l_t l_d l_yp l_d23;
run;
/*when there is no effect of an agreement, dummy agreement variables are equal to 0*/
data rev.freq_effets9401_totb;
set rev.freq_effets9401_totb;
if indic_cd=. then indic_cd=0;
if indic_cg=. then indic_cg=0;
if indic_eff_br=. then  indic_eff_br=0;
if indic_eff_e=. then  indic_eff_e=0; 
if indic_eff =. then indic_eff=0;
run;
/************* The data set contains all firms observations at a monthly frequency + a dummy variable equal to one when there is an effect of an 
industry or a firm-level agreement at a given date (month+year)********/


/**********Frequency of effects of wage agreements (without minimum wage **********/
proc freq data=rev.freq_effets9401_totb;
tables indic_eff;
weight yp;
run;
/*firm level agreement effect*/
proc freq data=rev.freq_effets9401_totb;
tables indic_eff_e;
weight yp;
run;
/*industry level agreement effect*/
proc freq data=rev.freq_effets9401_totb;
tables indic_eff_br;
weight yp;
run;

/******************************************************************/
/* Compute Frequency of Wage agreements (WITH NMW)*****************/
/******************************************************************/

/*on repart des bases de dur�es*/
data duree_effet_br9401_f;
set rev.duree_effet_br9401;
indic_eff_br=1;
drop an l_an l_mois;
run;
data duree_eff_ents9401_f;
set rev.duree_eff_ents9401;
indic_eff_e=1;
drop an l_an l_mois;
run;

data duree_eff_debfin9401_f;
set rev.duree_eff_debfin9401;
indic_eff=0;
drop an l_an l_mois;
run;
/****************keep all dates of NMW changes for all firms************/
data duree_eff_s9401_f;
set rev.duree_effet_smic9401;
indic_eff_s=1;
drop an l_an l_mois;
run;

/*STACK four data sets*/
data duree_eff_br_ents_s9401_f;
set duree_effet_br9401_f duree_eff_ents9401_f duree_eff_debfin9401_f duree_eff_s9401_f; 
drop an l_an l_mois;
run;
data duree_eff_br_ents_s9401_f;
set duree_eff_br_ents_s9401_f;
if siren='         ' then delete;
run;
proc sort data=duree_eff_br_ents_s9401_f;
by siren ann_eff mois_eff ;
run;
/*define censoring (left or right) and define a variable "indic_effc" equal to one if there is an effect of a wage agreement (industry or firm)*/
data duree_eff_br_ents_s9401_f;
set duree_eff_br_ents_s9401_f;
indic_cd=0;
indic_cg=0;
if indic_deb=1 then indic_cg=1;
if indic_fin=1 then indic_cd=1;

if indic_eff_br=. then  indic_eff_br=0;
if indic_eff_e=. then  indic_eff_e=0;
if indic_eff_e=0 and indic_eff_br=0 and indic_eff_s=0 then indic_eff=0;
if indic_eff_e=1 or indic_eff_br=1 or indic_eff_s=1 then indic_eff=1;
run;

data duree_eff_br_ents_s9401_f;
set duree_eff_br_ents_s9401_f;
keep D23 SIREN ann_eff code_dept indic_cd indic_cg  mois_eff t_ents_yp yp indic_eff_br indic_eff_e indic_eff_s indic_eff; 
run;
proc sort data=duree_eff_br_ents_s9401_f nodupkeys;
by siren ann_eff mois_eff ;
run;
data duree_eff_br_ents_s9401_f;
set duree_eff_br_ents_s9401_f;
rename ann_eff=an;
rename mois_eff=mois; 
run;


/************************/
/* FREQ****************/
/**********************/
data freq_effets_tot_s9401;
merge freq_effets9401_tot(in=in1) duree_eff_br_ents_s9401_f;
by siren an mois;
if in1;
run;
data freq_effets_tot_s9401;
set freq_effets_tot_s9401;
if indic_cd=. then indic_cd=0;
if indic_cg=. then indic_cg=0;
if indic_eff_br=. then  indic_eff_br=0;
if indic_eff_e=. then  indic_eff_e=0; 
if indic_eff =. then indic_eff =0;
run;
/****************************************************************/
/* We duplicate the whole data set creating two other data sets:
/* - one accounting for minimum wage earners: the data set will contain the proportion of NMW earners in a given industry 
for a given date and will be associated with all firms. We then will calculate the nb of employees in a given firm covered by NMW
	- one accounting for other workers: the data set will contain the proportion of other workers (100-%of NMW earners) in a given industry 
for a given date and will be associated with all firms. We then will calculate the nb of employees in a given firm not covered by NMW

Then we will stack the data sets and compute the weighted (by nb of employees) frequecy of wage agreements including NMW*/

/*duration.duree_effet_smic9401_1: contains for each firm*year the % of minimum wage earners in its industry * d�partement (geographical location)***/

data freq_effet_smic9401_1;
set rev.duree_effet_smic9401_1;
rename ann_eff=an;
run;
proc sort data=freq_effet_smic9401_1;
by siren an;
run;
proc sort data=freq_effets_tot_s9401;
by siren an;
run;
data freq_effet_smic9401_sb;
merge freq_effets_tot_s9401 freq_effet_smic9401_1;
by siren an;
run;
/*calculate the "theoretical number" of employees covered by NMW increase in a given firm as the number of empoyees (YP) 
																								* the % of minimum wage earners in a given (industry * year)*/
data freq_effet_smic9401_sb;
set freq_effet_smic9401_sb;
yp_smic=yp*p_smic2/100;
if indic_eff_s=. then indic_eff_s=0;
run;

/*duration.duree_effet_smic9401_1: contains for each firm*year the % of other workers in its industry * d�partement (geographical location)***/
data freq_effet_smic9401_0;
set rev.duree_effet_smic9401_0;
rename ann_eff=an;
run;
proc sort data=freq_effet_smic9401_0;
by siren an;
run;
proc sort data=rev.freq_effets9401_totb;
by siren an;
run;
data freq_effets9401_totc;
merge rev.freq_effets9401_totb freq_effet_smic9401_0;
by siren an;
run;
/*calculate the "theoretical number" of employees not covered by NMW increase in a given firm as the number of empoyees (YP) 
																								* (100-the % of minimum wage earners in a given (industry * year)*/

data freq_effets9401_totc;
set freq_effets9401_totc;
yp_smic=yp*p_smic2/100;
if indic_eff_s=. then indic_eff_s=0;
run;

/*we stack the two data set (covered or not by NMW*******/
data rev.freq_effet_smic9401_s10 ;
set freq_effets9401_totc freq_effet_smic9401_sb;
run;
proc sort data=rev.freq_effet_smic9401_s10;
by siren an mois;
run;



/****************************************/
/*Compute the frequency of wage agreements*/
/********************************************/
/*TABLE 1 and 2*/

/*TABLE 1 line 4**************************/
proc freq data=rev.freq_effet_smic9401_s10;
tables indic_eff;
weight yp_smic;
run;





/* TABLE 2 third column BY industry (D23)*/
proc sort data=rev.freq_effet_smic9401_s10 out=test_secteur_eff ;
by d23;
run;

proc freq data=test_secteur_eff noprint ;
tables indic_eff / out=freq_secteur_eff;
by d23;
weight yp_smic;
run;
proc means data=test_secteur_eff noprint ;
var yp_smic ;
by d23;
output out=  poids_secteur mean=poids_sect;
run;

data freq_secteur_eff;
set freq_secteur_eff;
if indic_eff=0 and percent=100 then indic_eff=1 ;
if indic_eff=0 and percent=100 then percent=0 ;
run;

data freq_secteur_eff;
set freq_secteur_eff;
if indic_eff=0 then delete;
run;

data freq_secteur_eff2;
merge freq_secteur_eff poids_secteur (in=in1);
by d23;
if in1;
run;
data freq_secteur_eff2;
set freq_secteur_eff2;
sect=substr(d23,1,2);
run;
data freq_secteur_eff2;
set  freq_secteur_eff2;
/*
A	1	2
B	5	
C	10	14
D	15	37
E	40	41
F	45	
G	50	52
H	55	
I	60	64
J	65	67
K	70	74
L	75	
M	80	
N	85	
O	90	93
P	95	97
Q	99	
*/
if sect ge 1 and sect le 2 then s='A';
if sect ge 5 and sect le 5 then s='B';
if sect ge 10 and sect le 14 then s='C';
if sect ge 15 and sect le 37 then s='D';
if sect ge 40 and sect le 41 then s='E';
if sect ge 45 and sect le 45 then s='F';
if sect ge 50 and sect le 52 then s='G';
if sect ge 55 and sect le 55 then s='H';
if sect ge 60 and sect le 64 then s='I';
if sect ge 65 and sect le 67 then s='J';
if sect ge 70 and sect le 74 then s='K';

if sect ge 75 and sect le 75 then s='L';
if sect ge 80 and sect le 80 then s='M';
if sect ge 85 and sect le 85 then s='N';

if sect ge 90 and sect le 93 then s='O';
if sect ge 95 and sect le 97 then s='P';
if sect ge 99 and sect le 99 then s='Q';

ss='S';
if s in('A', 'B', 'C') then ss='A';
if s in('D', 'E') then ss='I';

if s in('F') then ss='J';


/*
if sect ge 90 and sect le 93 then s='R';

if sect ge 94 and sect le 96 then s='S';
if sect ge 97 and sect le 98 then s='T';
if sect ge 99 and sect le 99 then s='U';*/
run;
proc sort data=freq_secteur_eff2;
by ss;
run;

/**********TABLE 2 column 3 ***********/
proc univariate data=freq_secteur_eff2;
var percent;
weight poids_sect;
by ss;
output out=sortie_sect_eff mean=moy median=med q1=q1 q3=q3 n=nb;
run;
/**********TABLE 2 column 4 ***********/
data sortie_sect_eff;
set sortie_sect_eff;
dur=1/(moy/100);
run;
/*
proc univariate data=freq_secteur_eff2;
var percent;
weight poids_sect;
run;*/

/* By size */
proc sort data=rev.freq_effet_smic9401_s10 out=test_taille_eff;
by t_ents_yp;
run;
/* TABLE 2 third column by size */
proc freq data=test_taille_eff noprint ;
tables indic_eff / out=freq_taille_eff;
by t_ents_yp;
weight yp_smic;
run;

data freq_taille_eff;
set freq_taille_eff;
if indic_eff=0 then delete;
run;
/**********TABLE 2 column 4 ***********/
data freq_taille_eff;
set freq_taille_eff;
dur=1/(moy/100);
run;





/*************TABLE 6 EFFECTS ********************************/
/*sort table by year month*/
proc sort data=rev.freq_effet_smic9401_s10 out=test_m_a;
by an mois;
run;

/*compute the frequency of wage changes by date*/
proc freq data=test_m_a ;
tables indic_eff  / out=rev.res_f_eff9401_an_mois noprint;
weight yp_smic;
by an mois;
run;
/*firm-level*/
proc freq data=test_m_a ;
tables indic_eff_e  / out=rev.res_f_eff9401_e_an_mois noprint;
weight yp_smic;
by an mois;
run;
/*industry-level*/
proc freq data=test_m_a ;
tables indic_eff_br  / out=rev.res_f_eff9401_br_an_mois noprint;
weight yp_smic;
by an mois;
run;


data rev.res_f_eff9401_br_an_mois;
set rev.res_f_eff9401_br_an_mois;
if indic_eff_br=0 then delete;
drop count;
rename percent=f_br;
run;
data rev.res_f_eff9401_e_an_mois;
set rev.res_f_eff9401_e_an_mois;
if indic_eff_e=0 then delete;
drop count;
rename percent=f_e;
run;
data rev.res_f_eff9401_an_mois;
set rev.res_f_eff9401_an_mois;
if indic_eff=0 then delete;
drop count;
rename percent=f_tot;
run;
data rev.res_f_eff9401_an_mois_tot;
merge rev.res_f_eff9401_an_mois rev.res_f_eff9401_br_an_mois rev.res_f_eff9401_e_an_mois;
by an mois;
run;
data rev.res_f_eff9401_an_mois_tot;
set rev.res_f_eff9401_an_mois_tot;
drop indic_eff_br indic_eff_e indic_eff;
run;

/*compute the frequency of wage changes by month*/
/*TABLE 6 column 6*/
proc sort data=rev.res_f_eff9401_an_mois_tot;
by mois;
run;
proc means data=rev.res_f_eff9401_an_mois_tot;
var f_tot f_br f_e;
by mois;
run;




proc sort data=rev.freq_effet_smic9401_s10 nodupkeys out=test_m_firm;
by siren an mois;
run;
proc sort data=test_m_firm;
by mois;
run;
/*compute the proportion of firm-level wage agreement effect by month*/
/*TABLE 6 column 5*/
proc freq data=test_m_firm ;
tables indic_eff_e  / out=rev.res_f_eff9401_mois_firm noprint;
weight yp_smic;
by mois;
run;
/*compute the proportion of industry-level wage agreement effect by month*/
/*TABLE 6 column 4*/

proc freq data=test_m_firm ;
tables indic_eff_br  / out=rev.res_f_eff9401_mois_industry noprint;
weight yp_smic;
by mois;
run;
